<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="序号" prop="sortNum">
        <el-input-number v-model="dataForm.sortNum" placeholder="序号"></el-input-number>
      </el-form-item>
      <el-form-item label="年级名称" prop="gradeName">
        <el-input v-model="dataForm.gradeName" placeholder="年级名称"></el-input>
      </el-form-item>

      <el-form-item label="学校类型" prop="schoolType">
        <el-select v-model="dataForm.schoolType" placeholder="请选择学校类型">
          <el-option v-for="item in schoolTypeOptions" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="memo">
        <el-input v-model="dataForm.memo" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { getObj, addObj, putObj } from '@/api/pvm/grade'
  import { mapGetters } from 'vuex'
  import { remote } from '@/api/admin/dict'
  export default {
    data() {
      return {
        visible: false,
        schoolTypeOptions: [],
        dataForm: {
          gradeName: '',
          classCount: '',
          schoolType: '',
          memo: '',
          sortNum: ''
        },
        dataRule: {
          gradeName: [{required: true,message: '年级名称不能为空',trigger: 'blur'}],
          schoolType: [{required: true,message: '学校类型不能为空',trigger: 'blur'}]
        }
      }
    },
    computed: {
      ...mapGetters(['bizData', 'keyCollapse'])
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        remote('school_type').then(response => {
          this.schoolTypeOptions = response.data.data
        })
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            } else {
              addObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            }
          }
        })
      }
    }
  }
</script>
